<?php  if ( ! defined('DLFMPATH')) exit('No direct script access allowed');

include DLFMPATH . '/libraries/' . 'db.class.php';
include BASEPATH . '/config/' . 'database.php';

class Model extends DB
{
    var $tbl_name = "";
    
    function __construct() {
	GLOBAL $database;
	
	$use = $database['used'];
	//$this->db = new fDatabase($database[$use]['type'] , $database[$use]['name'] , $database[$use]['user'] , $database[$use]['pass'] , $database[$use]['port'], $database[$use]['timeout']);
	parent::__construct($database[$use]['name'] , $database[$use]['host'] , $database[$use]['user'] , $database[$use]['pass']);
	$this->set_charset($database[$use]['charset']);
    }
    
    function set_charset($charset='utf8'){
	$this->query("SET character_set_results={$charset}");
	//$this->query("SET collation_connection=utf8_general_ci");
	$this->query("SET NAMES '{$charset}'");
    }
    
    /** Convenient method for mysql_fetch_object().
      * @param $result The ressource returned by query(). If NULL, the last result returned by query() will be used.
      * @return An object representing a data row.
      */
    function result_array($result = NULL)
    {
      if ($result == NULL)
        $result = $this->lastResult;

      if ($result == NULL || mysql_num_rows($result) < 1)
        return array();
      else
	
	$array = array();
	$i = 0;
	while($row = mysql_fetch_assoc($result)){
	    foreach($row as $key=>$val){
		$array[$i][$key] = $val;
	    }
	    $i++;
	}
	
        return $array;
    }
    
    /** Convenient method for mysql_fetch_object().
      * @param $result The ressource returned by query(). If NULL, the last result returned by query() will be used.
      * @return An object representing a data row.
      */
    function queryUniqueRow($query, $debug = -1)
    {
	$result = $this->queryUniqueObject($query, $debug);
	$array = array();
	foreach($result as $key=>$val){
	    $array[$key] = $val;
	}
	
        return $array;
    }
    
    function set_table_name($table_name){
	$this->tbl_name = $table_name;
    }
    
    function get_table_name(){
	return $this->tbl_name;
    }
    
    function set_free_result($query) {
	if($query) $query->free_result();
    }
    
    function fetch_data($query){
	$result = "";
	if($query->num_rows() > 0){
	    foreach($query->result_array() as $row){
		$result[] = $row;
	    }
	}
	return $result;
    }
    
    function fetch_data_with_desc($query){
	$result = "";
	if($query->num_rows() > 0){
	    foreach($query->result_array() as $row){
		$row["num_rows"] = $query->num_rows() ;
		$result[] = $row;
	    }
	}
	return $result;
    }

    function load_record($fullload=false) {
	$query = $this->db->get($this->tbl_name);
	//echo "<br>sql : ".$this->db->last_query();
	$result =  ($fullload) ? $this->fetch_data_with_desc($query) : $this->fetch_data($query);
	$this->set_free_result($query);
	if(is_var_array($result) && count($result) == 1){
	    return $result[0];
	}else{
	    return "";
	}
    }

    function load_records($fullload=false) {
	$query = $this->db->get($this->tbl_name);
	//echo "<br>sql : ".$this->db->last_query();
	$result =  ($fullload) ? $this->fetch_data_with_desc($query) : $this->fetch_data($query);
	$this->set_free_result($query);
	return $result;
    }
    
    function set_order_by($data=""){
	if( is_var_array($data) ){
	    foreach($data as $key => $value){
		$this->db->order_by($key,$value);
	    }
	}else if( !is_blank($data) ){
	    $this->db->order_by($data);
	}
    }
    
    function set_group_by($data="") {
	if( !empty($data) ){
	    $this->db->group_by($data);
	}
    }
    
    function set_where($data){
	if( is_var_array($data) ){
	    $where_str = "( ";
	    $i=0;
	    foreach($data as $key => $value){
		if($i != 0) $where_str .= " AND ";
		$where_str .= " $key = '$value' ";
		$i++;
		//$this->db->where($key,$value);
	    }
	    $where_str .= " )";
	    $this->db->where($where_str);
	}else if( !is_blank($data) ){
	    $this->db->where($data);
	}
    }

    function set_where_not_equal($data){
	if( is_var_array($data) ){
	    foreach($data as $key => $value){
		$this->db->where($key." != ",$value);
	    }
	}else if( !is_blank($data) ){
	    $this->db->where($data);
	}
    }

    function set_where_in($data){
	if( is_var_array($data) ){
	    foreach($data as $key => $value){
		$this->db->where_in($key,$value);
	    }
	}
    }
    
    function set_like($data,$wc_postion="both"){
	if( is_var_array($data) ){
	    $where_str = "( ";
	    $i=0;
	    foreach($data as $key => $value){
		if($i != 0) $where_str .= " AND ";
		switch($wc_postion){
		    case "both" : $where_str .= " $key like '%$value%' "; break;
		    case "left" : $where_str .= " $key like '%$value' "; break;
		    case "right" : $where_str .= " $key like '$value%' "; break;
		    default : $where_str .= " $key like '%$value%' "; break;
		}
		$i++;
	    }
	    $where_str .= " )";
	    $this->db->where($where_str);
	}
    }
    
    function set_or_like($data,$wc_postion="both"){
	if( is_var_array($data) ){
	    $where_str = "( ";
	    $i=0;
	    foreach($data as $key => $value){
		if($i != 0) $where_str .= " OR ";
		switch($wc_postion){
		    case "both" : $where_str .= " $key like '%$value%' "; break;
		    case "left" : $where_str .= " $key like '%$value' "; break;
		    case "right" : $where_str .= " $key like '$value%' "; break;
		    default : $where_str .= " $key like '%$value%' "; break;
		}
		$i++;
	    }
	    $where_str .= " )";
	    $this->db->where($where_str);
	}
    }
    
    function set_not_like($data,$wc_postion="both"){
	if( is_var_array($data) ){
	    $where_str = "( ";
	    $i=0;
	    foreach($data as $key => $value){
		if($i != 0) $where_str .= " AND ";
		switch($wc_postion){
		    case "both" : $where_str .= " $key not like '%$value%' "; break;
		    case "left" : $where_str .= " $key not like '%$value' "; break;
		    case "right" : $where_str .= " $key not like '$value%' "; break;
		    default : $where_str .= " $key not like '%$value%' "; break;
		}
		$i++;
	    }
	    $where_str .= " )";
	    $this->db->where($where_str);
	}
    }
    
    function set_or_not_like($data,$wc_postion="both"){
	if( is_var_array($data) ){
	    $where_str = "( ";
	    $i=0;
	    foreach($data as $key => $value){
		if($i != 0) $where_str .= " OR ";
		switch($wc_postion){
		    case "both" : $where_str .= " $key not like '%$value%' "; break;
		    case "left" : $where_str .= " $key not like '%$value' "; break;
		    case "right" : $where_str .= " $key not like '$value%' "; break;
		    default : $where_str .= " $key not like '%$value%' "; break;
		}
		$i++;
	    }
	    $where_str .= " )";
	    $this->db->where($where_str);
	}
    }
    
    function set_limit($limit_start=0,$no_record=0){
	    if( !is_blank($limit_start) && !is_blank($no_record) ){
		    $this->db->limit($no_record, $limit_start);
	    }else if( !is_blank($no_record) ){
		    $this->db->limit($no_record);
	    }
    }

    function insert_record($data)
    {
	if($this->db->field_exists('created_date',$this->tbl_name)) $data['created_date'] = get_db_now();
	if($this->db->field_exists('created_by',$this->tbl_name)) $data['created_by'] = getSessionUserAid();
	$this->db->insert($this->tbl_name, $data);
	return $this->db->insert_id();
    }

    function update_record($data)
    {
	if( !is_var_array($data) ) return "";
	if($this->db->field_exists('updated_date',$this->tbl_name)) $data['updated_date'] = get_db_now();
	// if($this->db->field_exists('updated_by',$this->tbl_name)) $data['updated_by'] = getSessionUserAid();
	return $this->db->update($this->tbl_name, $data);
    }
    
    function delete_records()
    {
	$this->db->delete($this->tbl_name);
    }
}

class DL_Model extends Model
{
    var $db = "";
    function __construct() {
	$this->db = new Model;
    }
}

/* End of file DL_Model.class.php */
/* Location: ./core/DL_Model.class.php */